package com.ruoyi.mp.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.mp.service.IMpAccountService;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.card.WxMpCardCreateRequest;
import me.chanjar.weixin.mp.bean.card.WxMpCardLandingPageCreateRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <h3>springcloudSaas</h3>
 * <p>卡卷相关接口</p>
 *
 * @author : 郎兴宇
 * @date : 2024-09-29 16:04
 **/
@RestController
@RequestMapping("/card")
public class MpCardController extends BaseController {

    @Autowired
    private WxMpService wxService;
    @Autowired
    private IMpAccountService iMpAccountService;

    /**
     * 添加测试白名单
     *
     * @param id       小程序ID
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @PostMapping("/addTestWhiteList/{id}")
    @RequiresPermissions("mp:card:addTestWhiteList")
    @Log(title = "添加测试白名单", businessType = BusinessType.INSERT)
    public AjaxResult addTestWhiteList(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String openid = "adsad";
        return AjaxResult.success(wxService.switchoverTo(appId).getCardService().addTestWhiteList(openid));
    }



    /**
     * 核查code接口
     *
     * @param id       小程序ID
     *
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @PostMapping("/cardCodeCheckcode/{id}")
    @RequiresPermissions("mp:card:cardCodeCheckcode")
    @Log(title = "核查code接口", businessType = BusinessType.UPDATE)
    public AjaxResult cardCodeCheckcode(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String cardId= "";
        List<String> codeList = new ArrayList<>();
        return AjaxResult.success(wxService.switchoverTo(appId).getCardService().cardCodeCheckcode(cardId,codeList));
    }


    /**
     * 导入自定义code(仅对自定义code商户)
     *
     * @param id       小程序ID
     *
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @PostMapping("/cardCodeDeposit/{id}")
    @RequiresPermissions("mp:card:cardCodeDeposit")
    @Log(title = "导入自定义code(仅对自定义code商户)", businessType = BusinessType.INSERT)
    public AjaxResult 	cardCodeDeposit(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String cardId= "";
        List<String> codeList = new ArrayList<>();
        return AjaxResult.success(wxService.switchoverTo(appId).getCardService().cardCodeDeposit(cardId,codeList));
    }


    /**
     * 查询导入code数目接口
     *
     * @param id       小程序ID
     *
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @GetMapping("/cardCodeDepositCount/{id}")
    @RequiresPermissions("mp:card:cardCodeDepositCount")
    @Log(title = "查询导入code数目接口", businessType = BusinessType.EXPORT)
    public AjaxResult 	cardCodeDepositCount(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String cardId= "";
        return AjaxResult.success(wxService.switchoverTo(appId).getCardService().cardCodeDepositCount(cardId));
    }


    /**
     * 更改Code接口
     *
     * @param id       小程序ID
     *
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @PutMapping("/cardCodeUpdate/{id}")
    @RequiresPermissions("mp:card:cardCodeUpdate")
    @Log(title = "更改Code接口", businessType = BusinessType.UPDATE)
    public AjaxResult 	cardCodeUpdate(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String cardId= "";
        String oldCode = ""; String  newCode = "";
        wxService.switchoverTo(appId).getCardService().cardCodeUpdate(cardId,oldCode,newCode);
        return AjaxResult.success(1);
    }


    /**
     * 修改库存接口
     *
     * @param id       小程序ID
     *
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @PutMapping("/cardModifyStock/{id}")
    @RequiresPermissions("mp:card:cardModifyStock")
    @Log(title = "修改库存接口", businessType = BusinessType.UPDATE)
    public AjaxResult 	cardModifyStock(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String cardId= "";
        Integer changeValue= 1;
        wxService.switchoverTo(appId).getCardService().cardModifyStock(cardId,changeValue);
        return AjaxResult.success(1);
    }


    /**
     * 图文消息群发卡券获取内嵌html
     *
     * @param id       小程序ID
     *
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @PostMapping("/cardMpnewsGethtml/{id}")
    @RequiresPermissions("mp:card:cardMpnewsGethtml")
    @Log(title = "图文消息群发卡券获取内嵌html", businessType = BusinessType.INSERT)
    public AjaxResult 	cardMpnewsGethtml(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String cardId= "";
        return AjaxResult.success(wxService.switchoverTo(appId).getCardService().cardMpnewsGethtml(cardId));
    }

    /**
     * 设置买单接口
     *
     * @param id       小程序ID
     *
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @PostMapping("/cardPaycellSet/{id}")
    @RequiresPermissions("mp:card:cardPaycellSet")
    @Log(title = "设置买单接口", businessType = BusinessType.INSERT)
    public AjaxResult 	cardPaycellSet(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String cardId= "";
        Boolean isOpen = true;
        wxService.switchoverTo(appId).getCardService().cardPaycellSet(cardId,isOpen);
        return AjaxResult.success(1);
    }




    /**
     * 设置自助核销
     *
     * @param id       小程序ID
     *
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @PostMapping("/cardSelfConsumeCellSet/{id}")
    @RequiresPermissions("mp:card:\tcardSelfConsumeCellSet")
    @Log(title = "设置自助核销", businessType = BusinessType.INSERT)
    public AjaxResult  cardSelfConsumeCellSet(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String cardId= "";
        Boolean isOpen = true;
        Boolean needVerifyCod = true;
         Boolean needRemarkAmount = true;
        wxService.switchoverTo(appId).getCardService().	cardSelfConsumeCellSet(cardId,isOpen,needVerifyCod,needRemarkAmount);
        return AjaxResult.success(1);
    }


    /**
     * 卡券Code核销。
     *
     * @param id       小程序ID
     *
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @PostMapping("/consumeCardCode/{id}")
    @RequiresPermissions("mp:card:consumeCardCode")
    @Log(title = "卡券Code核销。", businessType = BusinessType.INSERT)
    public AjaxResult 	consumeCardCode(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String cardId= "";
        String code= "";
        return AjaxResult.success(wxService.switchoverTo(appId).getCardService().consumeCardCode(code,cardId));
    }


    /**
     * 创建卡券.。
     *
     * @param id       小程序ID
     *
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @PostMapping("/createCard/{id}")
    @RequiresPermissions("mp:card:createCard")
    @Log(title = "创建卡券", businessType = BusinessType.INSERT)
    public AjaxResult createCard(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        WxMpCardCreateRequest cardCreateMessage = new WxMpCardCreateRequest();
        return AjaxResult.success(wxService.switchoverTo(appId).getCardService().createCard(cardCreateMessage));
    }


    /**
     * 创建调用卡券api时所需要的签名..。
     *
     * @param id       小程序ID
     *
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @PostMapping("/createCardApiSignature/{id}")
    @RequiresPermissions("mp:card:createCardApiSignature")
    @Log(title = "创建调用卡券api时所需要的签名.", businessType = BusinessType.INSERT)
    public AjaxResult createCardApiSignature(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String optionalSignParam = "";
        return AjaxResult.success(wxService.switchoverTo(appId).getCardService().createCardApiSignature(optionalSignParam));
    }


    /**
     * 创建卡券货架.
     *
     * @param id       小程序ID
     *
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @PostMapping("/createLandingPage/{id}")
    @RequiresPermissions("mp:card:createLandingPage")
    @Log(title = "创建卡券货架", businessType = BusinessType.INSERT)
    public AjaxResult createLandingPage(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        WxMpCardLandingPageCreateRequest createRequest = new WxMpCardLandingPageCreateRequest();
        return AjaxResult.success(wxService.switchoverTo(appId).getCardService().createLandingPage(createRequest));
    }


    /**
     * 创建卡券二维码.
     *
     * @param id       小程序ID
     *
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @PostMapping("/createQrcodeCard/{id}")
    @RequiresPermissions("mp:card:createQrcodeCard")
    @Log(title = "创建卡券二维码", businessType = BusinessType.INSERT)
    public AjaxResult 	createQrcodeCard(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String cardId ="";
        String outerStr ="";
        int expiresIn = 1;
        String openid ="";
        String code ="";
        boolean isUniqueCode = true;
        return AjaxResult.success(wxService.switchoverTo(appId).getCardService().createQrcodeCard(cardId,outerStr,expiresIn,openid,code,isUniqueCode));
    }

    /**
     * 卡券Code解码.
     *
     * @param id       小程序ID
     *
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @PostMapping("/decryptCardCode/{id}")
    @RequiresPermissions("mp:card:decryptCardCode")
    @Log(title = "卡券Code解码", businessType = BusinessType.INSERT)
    public AjaxResult 	decryptCardCode(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String encryptCode ="";
        return AjaxResult.success(wxService.switchoverTo(appId).getCardService().decryptCardCode(encryptCode));
    }

    /**
     * 删除卡券接口.
     *
     * @param id       小程序ID
     *
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @PostMapping("/deleteCard/{id}")
    @RequiresPermissions("mp:card:deleteCard")
    @Log(title = "删除卡券接口", businessType = BusinessType.INSERT)
    public AjaxResult 	deleteCard(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String cardId ="";
        return AjaxResult.success(wxService.switchoverTo(appId).getCardService().deleteCard(cardId));
    }



    /**
     * 获得卡券api_ticket..
     *
     * @param id       小程序ID
     *
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @GetMapping("/getCardApiTicket/{id}")
    @RequiresPermissions("mp:card:getCardApiTicket")
    @Log(title = "获得卡券api_ticket.", businessType = BusinessType.EXPORT)
    public AjaxResult 	getCardApiTicket(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        boolean forceRefresh = true;
        return AjaxResult.success(wxService.switchoverTo(appId).getCardService().getCardApiTicket(forceRefresh));
    }


    /**
     * 查看卡券详情接口...
     *
     * @param id       小程序ID
     *
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @GetMapping("/getCardDetail/{id}")
    @RequiresPermissions("mp:card:getCardApiTicket")
    @Log(title = "查看卡券详情接口..", businessType = BusinessType.EXPORT)
    public AjaxResult 	getCardDetail(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String cardId = "";
        return AjaxResult.success(wxService.switchoverTo(appId).getCardService().getCardDetail(cardId));
    }


    /**
     * 获取用户已领取卡券接口
     *
     * @param id       小程序ID
     *
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @GetMapping("/getUserCardList/{id}")
    @RequiresPermissions("mp:card:getUserCardList")
    @Log(title = "获取用户已领取卡券接口", businessType = BusinessType.EXPORT)
    public AjaxResult 	getUserCardList(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String cardId = "";
        String openId = "";
        return AjaxResult.success(wxService.switchoverTo(appId).getCardService().getUserCardList(openId,cardId));
    }


    /**
     * 卡券Mark接口.
     *
     * @param id       小程序ID
     *
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @GetMapping("/markCardCode/{id}")
    @RequiresPermissions("mp:card:markCardCode")
    @Log(title = "卡券Mark接口.", businessType = BusinessType.EXPORT)
    public AjaxResult 	markCardCode(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String code  ="";
        String cardId = "";
        String openId = "";
        boolean isMark = true;
        wxService.switchoverTo(appId).getCardService().markCardCode(code,cardId,openId,isMark);
        return AjaxResult.success(1);
    }


    /**
     * 卡券Mark接口.
     *
     * @param id       小程序ID
     *
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @GetMapping("/queryCardCode/{id}")
    @RequiresPermissions("mp:card:queryCardCode")
    @Log(title = "卡券Code查询", businessType = BusinessType.EXPORT)
    public AjaxResult 	queryCardCode(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String cardId = "";
        String code  ="";
        boolean checkConsume = true;
        wxService.switchoverTo(appId).getCardService().queryCardCode(cardId,code,checkConsume);
        return AjaxResult.success(1);
    }



    /**
     * 将用户的卡券设置为失效状态..
     *
     * @param id       小程序ID
     *
     * @throws WxErrorException 若微信请求发生错误异常
     */
    @GetMapping("/unavailableCardCode/{id}")
    @RequiresPermissions("mp:card:unavailableCardCode")
    @Log(title = "将用户的卡券设置为失效状态.", businessType = BusinessType.EXPORT)
    public AjaxResult 	unavailableCardCode(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String cardId = "";
        String code  ="";
        String reason = " ";
        return AjaxResult.success(wxService.switchoverTo(appId).getCardService().unavailableCardCode(cardId,code,reason));
    }
}


